package com.qiantu.phone.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qiantu.phone.R;

public class SwipeMoreMenuView extends FrameLayout {
    private FrameLayout touchLayout;
    private GestureDetector gestureDetector;
    private int menuLocation;
    private View menuLayout;
    private MenuListener menuListener;
    private boolean showMenu;//是否在显示菜单

    public SwipeMoreMenuView(@NonNull Context context) {
        super(context);
    }

    public SwipeMoreMenuView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                float translationX = touchLayout.getTranslationX() - distanceX;
                if (menuLocation == 0) {
                    if (translationX < 0) {
                        //菜单在左边 只能往右滑
                        translationX = 0;
                    } else if (translationX > menuLayout.getWidth()) {
                        //滑过了菜单，停在菜单右边
                        translationX = menuLayout.getWidth();
                    }
                } else if (menuLocation == 1) {
                    if (translationX > 0) {
                        //菜单在右边 只能往左滑
                        translationX = 0;
                    } else if (translationX < -menuLayout.getWidth()) {
                        //滑过了菜单，停在菜单左边
                        translationX = -menuLayout.getWidth();
                    }

                }
                touchLayout.setTranslationX(translationX);
                return super.onScroll(e1, e2, distanceX, distanceY);
            }

            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }
        });
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SwipeMoreMenuView, 0, 0);
        menuLocation = array.getInt(R.styleable.SwipeMoreMenuView_menu_location, 1);
        menuLayout = View.inflate(context, array.getResourceId(R.styleable.SwipeMoreMenuView_menu_layout_id, -1), null);
        View contentLayout = View.inflate(context, array.getResourceId(R.styleable.SwipeMoreMenuView_content_layout_id, -1), null);
        FrameLayout.LayoutParams menuLp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuLp.gravity = Gravity.CENTER_VERTICAL | (menuLocation == 0 ? Gravity.LEFT : Gravity.RIGHT);//水平居中 并根据参数居左或右
        menuLayout.setLayoutParams(menuLp);
        addView(menuLayout);

        contentLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        touchLayout = new FrameLayout(context);
        touchLayout.setBackgroundColor(array.getColor(R.styleable.SwipeMoreMenuView_content_background_color, 0));
        touchLayout.setOnClickListener(v -> {
        });
        addView(touchLayout);

        touchLayout.addView(contentLayout);
        touchLayout.post(new Runnable() {
            @Override
            public void run() {
                touchLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, menuLayout.getHeight()));
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if ((ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL)
                && touchLayout.getTranslationX() != 0) {
            float translationEndX = 0f;
            float translationX = touchLayout.getTranslationX();
            if (menuLocation == 0) {
                if (translationX < menuLayout.getWidth() / 2f) {//停下的位置小于菜单的一半
                    translationEndX = 0;
                } else {
                    translationEndX = menuLayout.getWidth();//大于一半 开启菜单
                }
            } else if (menuLocation == 1) {
                if (translationX > -menuLayout.getWidth() / 2f) {//停下的位置小于菜单的一半
                    translationEndX = 0;
                } else {
                    translationEndX = -menuLayout.getWidth();//大于一半 开启菜单
                }
            }
            showMenu = translationEndX != 0;//移动的X坐标为0说明菜单被遮住了
            ObjectAnimator.ofFloat(touchLayout, "translationX",
                    translationX, translationEndX).setDuration(200).start();
        }
        if (showMenu) {
            if (menuLocation == 0 && ev.getX() < menuLayout.getWidth()) {
                return super.dispatchTouchEvent(ev);
            } else if (menuLocation == 1 && ev.getX() > menuLayout.getLeft()) {
                return super.dispatchTouchEvent(ev);
            }
        }
        return gestureDetector.onTouchEvent(ev);
    }

    public void setMenuClickIdsListener(MenuListener menuListener, int... ids) {
        this.menuListener = menuListener;
        for (int id : ids) {
            findViewById(id).setOnClickListener(v -> {
                this.menuListener.onClick(v);
            });
        }
    }

    public interface MenuListener {
        void onClick(View v);
    }
}
